home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Communication / NGService / Source / MMCell.m < prev    next >
Text File  |  1993-09-02  |  5KB  |  222 lines

  1. /*
  2.  *      You may freely copy, distribute and reuse the code
  3.  *      in this example.  Scott Anguish disclaims any warranty of
  4.  *      any kind, expressed or implied, as to its fitness
  5.  *      for any particular use.
  6.  *        Please keep this notice intact
  7.  *      Written by: Scott Anguish  sanguish@digifix.com
  8.  */
  9.  
  10. #import "MMCell.h"
  11.  
  12. #import <libc.h>                      /* MAXPATHLEN */
  13. #import <stdio.h>
  14. #import <strings.h>
  15. #include <sys/types.h>
  16. #include <ctype.h>
  17. #include <stdlib.h>
  18.  
  19. #import <appkit/appkit.h>
  20.  
  21. #import <streams/streams.h>
  22.  
  23.  
  24. @implementation MMCell:Cell
  25.  
  26. - initFromFilename:(const char *)filename
  27. {
  28.     [super init];
  29.     displayContents=NO;
  30.     [self imageFromFilename:filename];
  31.     return self;
  32. }
  33.  
  34.  
  35. - imageFromAttachment
  36. {
  37.     return self;
  38. }
  39.  
  40.  
  41. - imageFromFilename:(const char *)filename
  42. {
  43.     return self;
  44. }
  45.  
  46.  
  47.  
  48. - free
  49. {
  50.     fprintf(stderr,"Free\n");
  51.     [theImage free];
  52.     [self removeData];
  53.     [super free];
  54.     return self;
  55. }
  56.  
  57. - calcCellSize:(NXSize *)theSize
  58. {
  59.     NXSize s;
  60.     
  61.     [theImage getSize: &s];
  62.     s.height += 4.0;
  63.     s.width += 4.0;
  64.     *theSize = s;
  65.     return self;
  66. }
  67.  
  68. - highlight:(const NXRect *)cellFrame inView:controlView lit:(BOOL)flag;
  69. {
  70.     return self;
  71. }
  72.  
  73.  
  74. - writeRichText:(NXStream *)stream forView:view
  75. {
  76.  
  77.     
  78.     NXPrintf(stream, "NXPRINTF\n");
  79.     return self;
  80. }
  81.  
  82. - readPrivate:(NXStream *)stream name:(char *)name
  83. {
  84.     
  85.  
  86.     NXScanf(stream, "%s\n", name); 
  87.     strcpy(attachmentName,name);
  88.     NXScanf(stream, "%d ", &attachmentSize);
  89.     
  90.  
  91.     attachmentData=malloc(attachmentSize);
  92.     NXRead(stream, attachmentData,attachmentSize);
  93.     return self;
  94. }
  95.  
  96. - readRichText:(NXStream *)stream forView:view
  97. {
  98.     char name[MAXPATHLEN+1];
  99.  
  100.     MMView = view;
  101.     [self readPrivate:stream name:name];
  102.     [self uudecodeAttachment];
  103.     [self imageFromAttachment];
  104.  
  105.     return self;
  106. }
  107.  
  108. - (BOOL)trackMouse:(NXEvent *)theEvent 
  109.     inRect:(const NXRect *)cellFrame 
  110.     ofView:controlView;
  111. {
  112.     NXEvent eventBuf, *ev;
  113.     NXPoint thePoint;
  114.     NXRect theRect;
  115.     id localImage;
  116.     char filename[MAXPATHLEN+1];
  117.     NXAtom thetype;
  118.  
  119.     sprintf(filename,"/tmp/%s",attachmentName);
  120.  
  121.     if ((theEvent->data.mouse.click == 1)) {
  122.         ev = [NXApp peekNextEvent:NX_MOUSEDOWNMASK|NX_MOUSEUPMASK
  123.             into:&eventBuf waitFor:0.3 threshold:NX_MODALRESPTHRESHOLD];
  124.         if (!ev || ev->type == NX_MOUSEDRAGGED) {
  125.  
  126.             thePoint=theEvent->location;
  127.             [controlView convertPoint:&thePoint fromView:NULL];
  128.             NXSetRect(&theRect,thePoint.x-24,thePoint.y-24,48,48);
  129.             [controlView dragFile:filename fromRect:&theRect slideBack:YES event:theEvent];
  130.             return YES;
  131.         }
  132.     } 
  133.     else if ((theEvent->data.mouse.click == 2)) {
  134.         
  135.         [[Application workspace] getInfoForFile:filename application:NULL type:&thetype];
  136.         if ((thetype==NXApplicationFileType)||(thetype==NXShellCommandFileType))
  137.             {
  138.              if (NXRunAlertPanel("NGService","This is an executable, and could be a security breach. Go ahead?","Yes", "Cancel", 
  139.                                      NULL)==NX_ALERTALTERNATE){return YES;} 
  140.             }
  141.              
  142.         theRect = *cellFrame;
  143.         NX_Y(&theRect) += 48.0;
  144.         localImage=theImage;
  145.         if (displayContents) 
  146.             localImage=[[Application workspace] getIconForFile:filename];
  147.         [[Application workspace] openFile:filename fromImage:localImage at:&theRect.origin inView:controlView];
  148.         return YES;
  149.     }
  150.     return NO;
  151. }
  152.  
  153. - read:(NXTypedStream *)stream
  154. {
  155.     [super read:stream];
  156.     return self;
  157. };
  158.  
  159. - write:(NXTypedStream *)stream
  160. {
  161.     [super write:stream];
  162.     return self;
  163. };
  164.  
  165. - drawInside: (const NXRect *)cellFrame inView:controlView
  166. {
  167.     NXPoint    compositePlace;
  168.  
  169.  
  170.     compositePlace.x = cellFrame->origin.x+ 2.0;
  171.     compositePlace.y = NX_MAXY(cellFrame) + 2.0;
  172.     if (!theImage) NXFrameRect(cellFrame); else [theImage composite:NX_SOVER toPoint:&compositePlace];
  173.  
  174.     return self;
  175. }
  176.  
  177. - uudecodeAttachment
  178. {
  179.     FILE *fp;
  180.     char commandStr[BUFSIZ];
  181.     
  182.     sprintf(commandStr,"rm -f /tmp/%s.Z; /usr/bin/uudecode ",attachmentName);
  183.     fp = popen (commandStr,"w");
  184.     if (fp != (FILE *)NULL)
  185.         {
  186.         fprintf(fp,"begin 644 /tmp/%s.Z\n",attachmentName);
  187.         fwrite(attachmentData,sizeof(char),attachmentSize,fp);
  188.         fprintf(fp,"end\n");
  189.         fprintf(stderr,"Pipe Result %d \n",pclose(fp));
  190.         }
  191.         else
  192.         fprintf(stderr,"Pipe did not open \n");
  193.  
  194.     sprintf(commandStr,"rm -fR /tmp/%s; cd /tmp; zcat %s.Z | /NextApps/Mail.app/safetar xf -; rm -f %s.Z", attachmentName, attachmentName, attachmentName);
  195.     fp = popen (commandStr,"r");
  196.     if (fp != (FILE *)NULL)
  197.         {
  198.         fprintf(stderr,"Pipe Result %d \n",pclose(fp));
  199.         }
  200.         else
  201.         fprintf(stderr,"Pipe did not open \n");
  202.     return self;
  203. };
  204.  
  205. - removeData
  206. {
  207.     FILE *fp;
  208.     char commandStr[BUFSIZ];
  209.     
  210.     sprintf(commandStr,"rm -f %s", attachmentName);
  211.     fp = popen (commandStr,"r");
  212.     if (fp != (FILE *)NULL)
  213.         {
  214.         fprintf(stderr,"Pipe Result %d \n",pclose(fp));
  215.         }
  216.         else
  217.         fprintf(stderr,"Pipe did not open \n");
  218.     return self;
  219.  
  220. }
  221. @end
  222.